package com.xdhl.fu.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdhl.fu.member.domain.MemberBasic;
import com.xdhl.fu.member.domain.vo.TotalMemberVO;
import com.xdhl.fu.member.mapper.MemberBasicMapper;
import com.xdhl.fu.member.service.IMemberBasicService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 会员基本信息业务实现
 *
 * @author 朱力维
 * @time 2022/5/27 16:44
 */
@Service
public class MemberBasicServiceImpl extends ServiceImpl<MemberBasicMapper, MemberBasic> implements IMemberBasicService {

    @Resource
    private MemberBasicMapper memberBasicMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<MemberBasic> listActive(Integer month) {
        return memberBasicMapper.listActive(month);
    }

    /**
     * 按职业分组统计当日注册的会员
     *
     * @return 统计数据
     */
    @Override
    public List<TotalMemberVO> countGroupRegisteredToday() {
        List<TotalMemberVO> totalMemberVOS = memberBasicMapper.countGroupRegisteredToday();
        totalMemberVOS.forEach(item -> {
            String display = (String) stringRedisTemplate.opsForHash().get("dict:occupation_class", item.getOccupationClass());
            item.setOccupationClassDisplay(display);
        });
        return totalMemberVOS;
    }

    @Override
    public List<TotalMemberVO> countMemberActive7Days() {
        List<TotalMemberVO> totalMemberVOS = memberBasicMapper.countMemberActive7Days();
        return totalMemberVOS;
    }

    @Override
    public MemberBasic getByUsername(String username) {
        LambdaQueryWrapper<MemberBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MemberBasic::getEmail, username)
                .or().eq(MemberBasic::getTelephoneNumber, username)
                .or().eq(MemberBasic::getIdCardNumber, username);
        return memberBasicMapper.selectOne(wrapper);
    }

}
